package com.deep.product.controller.web;

import com.alibaba.fastjson.TypeReference;
import com.deep.common.utils.R;
import com.deep.product.annotation.ApiController;
import com.deep.product.feign.SeckillFeignService;
import com.deep.product.model.dto.SeckillSkuDTO;
import com.deep.product.model.entity.CategoryEntity;
import com.deep.product.model.vo.Catalog2VO;
import com.deep.product.service.CategoryService;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author Deep
 * @date 2022/1/25
 */
@ApiController  // /api/product
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private SeckillFeignService seckillFeignService;

    /**
     * 首页、返回一级目录
     *
     * @param model 视图
     * @return 首页
     */
    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> entities = categoryService.getLevel(0L, 1L);
        model.addAttribute("categorys", entities);

        // 秒杀商品
        List<SeckillSkuDTO> seckillSkuDTOS = seckillFeignService.getCurrentSeckillSkus();
        model.addAttribute("items", seckillSkuDTOS);
        return "index";
    }

    /**
     * 查出三级分类
     * 1级分类作为key，2级引用List
     */
    @GetMapping("index/catalog")
    @ResponseBody
    public Map<String, List<Catalog2VO>> getCatalogJson() {
        Map<String, List<Catalog2VO>> map = categoryService.getCatalogJson();
        return map;
    }

/*    @GetMapping("/lockDoor")
    @ResponseBody
    public String closeDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "锁门啦!";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id) {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id + ":go go go!";
    }

    @GetMapping("/product/gogogo")
    @ResponseBody
    public String gogogo() {
//        RCountDownLatch door = redissonClient.getCountDownLatch("door");
//        door.countDown();
        return "product:go go go!";
    }*/

}
